---
--类名: PhotoView.lua
--创建日期: 2018-7-30
--创建人: 宋建邦
--功能描述:拍照视图
--修改内容:
--修改日期:
--修改人:
--修改备注:

local PhotoMenuRender = import(".renders.PhotoMenuRender")
local IMTeamPanel = require("app.im.views.IMTeamPanel")

local Res = {
     PhotoView = "ui2/main/Scene_photo.csb",
}

local PhotoView = class("PhotoView", app.mvc.ViewBase)
PhotoView.VIEW_ROOT_RES = Res.PhotoView

function PhotoView:init()
    self:setBackGroundColorType(0)
    self:setNodeEventEnabled(true)

    local pc = app:getInst("PhotoController")
    self.pctl = pc 
    local ms = {pc.faceMenuData, pc.envMenuData, pc.frameMenuData, pc.filterMenuData, pc.logoMenuData}
    for i = 1, 5 do
        self._root:findChild("Panel/selected_btn_"..tostring(i)):addClickEventListener(function(sender)
            local idx = i
            if self.popMenu and not cc.isDead(self.popMenu) then
                self.popMenu:removeFromParent()
            end
            local m = PhotoMenuRender.createWithData(ms[i])
            self:addChild(m)            
            self.popMenu = m 
            m:linkTo(self._root:findChild("Panel/selected_btn_"..tostring(idx)))

            display.nodeRemoveForNotHit(m, function()
                if self.popMenu and not cc.isDead(self.popMenu) then
                    self.popMenu:removeFromParent()
                end
                self.popMenu = nil
            end)
        end)
    end

    self._root:findChild("Panel/btn_photo"):addClickEventListener(function(sender)
        self:snapshot()
        end)

    self._root:findChild("Panel/Button_close"):addClickEventListener(function(sender)
        self.pctl:closePhotoView()
        end)

    self:initCheckBox()

    if app:getPublisher() == Enums.Publisher.ema then
        self._root:findChild("Panel/Panel_btn"):hide()
    end

    self:addLogo()
    self:addFrame()
    self:addQRCode()
    self:showQRCode(false)
    UIHandler:hideDesktop()

    display.doLayout(self._root)
end

function PhotoView:onExit()
    app:sendMsg("IMSimpleController", "showView")
    UIHandler:showDesktop()
end

function PhotoView:initCheckBox()
    local p = self._root:findChild("Panel/Panel_tick")
    local b = self._root:findChild("Panel/Panel_tick/selected_btn")

    b:setTouchEnabled(true)

    b:addClickEventListener(function(sender)
        b:setTouchEnabled(false)
        b:setBright(false)
        self.pctl:hideOtherCharacter()
        end)
    p:setTouchEnabled(true)
    p:addTouchEventListener(function(sender,eventType)
        if eventType == ccui.TouchEventType.ended then
            b:setTouchEnabled(true)
            b:setBright(true)
            self.pctl:showAllCharacter()
        end
    end)

    local pn = self._root:findChild("Panel/Panel_name")
    local bn = self._root:findChild("Panel/Panel_name/selected_btn")

    bn:setTouchEnabled(true)

    local fshow = function(sender)
        bn:setTouchEnabled(false)
        bn:setBright(false)
        FieldManager:updateAllCharactersUpHead(true)
        EM:notify("photo.set.showname", true)
        end
    local fhide = function(sender,eventType)
        if eventType == ccui.TouchEventType.ended then
            bn:setTouchEnabled(true)
            bn:setBright(true)
            FieldManager:updateAllCharactersUpHead(false)
            EM:notify("photo.set.showname", false)
        end
    end
    bn:addClickEventListener(fshow)
    pn:setTouchEnabled(true)
    pn:addTouchEventListener(fhide)
    fshow()

    local c = self._root:findChild("Panel/btn_chat")
    c:addClickEventListener(function(sender)
        app:sendMsg("IMRoomController", "showView")
        end)
    c:hide()

    self._root:findChild("Panel/Panel_btn"):setVisible(false)

    -- self._teamPanel = IMTeamPanel.create(self._root:findChild("Panel/Panel_btn/node_set"))
    -- -- self._teamPanel:refresh(false)
    -- local hasMic, hasSpeaker = app:sendMsg("IMRecordController", "getMicSpeaker")
    -- self._teamPanel:refresh(app:getInst("TeamModel"):hasTeam(), hasMic, hasSpeaker)
    -- if not app:getInst("TeamModel"):hasTeam() then
    --     self._teamPanel:refresh(false)
    --     c:hide()
    --     -- p:hide()
    --     -- v:hide()
    --     -- t:hide()
    -- end
end

function PhotoView:snapshot()
    self:showQRCode(true)
    -- EM:notify("photo.before.snapshot")
    self.pctl:snapshot()

    -- print(self.splash)
    
    -- if not self.splash then
    --     self.splash = ccui.Layout:create()
    --     self.splash:setBackGroundColor(display.COLOR_WHITE)
    --     self.splash:setBackGroundColorOpacity(255)
    --     self.splash:setBackGroundColorType(1)
    --     self.splash:setContentSize(cc.size(display.width, display.height))
    --     self.splash:setAnchorPoint(0, 0)
    --     self:addChild(self.splash)
    --     self.splash:hide()
    -- end

    -- self.splash:stopAllActions()
    self._root:findChild("Panel"):hide()
    local act = cca.seq({cca.delay(0), cca.callFunc(function()
        -- self.splash:show()
        -- self.splash:setOpacity(255)
        -- self.splash:setBackGroundColorOpacity(255)
        self._root:findChild("Panel"):show()
        self:showQRCode(false)
    end), 
    -- cca.fadeIn(0.2),cca.delay(0.2), cca.hide(),
    cca.callFunc(function()
        self.pctl:closePhotoView()
        -- EM:notify("photo.after.snapshot")
        app:getInst("PhotoController"):showPhotoViewShare(true, app:sendMsg("PhotoController", "getPhotoModel"))    
    end) })

    self._root:findChild("Panel"):runAction(act)
    -- self.splash:runAction(act)
end

function PhotoView:addLogo()
    self.logo = ccui.ImageView:create("ui_resources/login/login_logo.png")
    self:addChild(self.logo)
    self.logo:hide()
end

function PhotoView:addQRCode()
    -- self.qrcode = ccui.ImageView:create("/art/photo/qrcode.png")
    -- self:addChild(self.qrcode)
    -- self.qrcode:setPosition(cc.p(100, 100))

    -- local x = 180
    -- local y = -50
    -- self.nameLbl = cc.Label:createWithTTF(UD:getNickName(), "ui2/fonts/LiheiPro.TTF", 20)
    -- self.nameLbl:setAnchorPoint(cc.p(0, 0))
    -- self.nameLbl:setTextColor(cc.c4b(255,225,132,255))
    -- self.nameLbl:setPosition(cc.p(x, y + 150))
    -- self:addChild(self.nameLbl)

    -- self.lvLbl = cc.Label:createWithTTF("lv "..tostring(UD.player.level), "ui2/fonts/LiheiPro.TTF", 20)
    -- self.lvLbl:setAnchorPoint(cc.p(0, 0))
    -- self.lvLbl:setTextColor(cc.c4b(255,225,132,255))
    -- self.lvLbl:setPosition(cc.p(x, y+125))
    -- self:addChild(self.lvLbl)

    -- self.zoneLbl = cc.Label:createWithTTF(L(UD.zoneName), "ui2/fonts/LiheiPro.TTF", 20)
    -- self.zoneLbl:setAnchorPoint(cc.p(0, 0))
    -- self.zoneLbl:setTextColor(cc.c4b(255,225,132,255))
    -- self.zoneLbl:setPosition(cc.p(x, y+100))
    -- self:addChild(self.zoneLbl)
    self.qrpanel = self.pctl:getQRCodePanel()
    self:addChild(self.qrpanel)
end

function PhotoView:showQRCode(b)
    if true then -- app:isTencent() and UD.msdkData:isNotTencentChannel() then
        local code = self.qrpanel:findChild("Panel/ewm")
        if code then
            code:setVisible(false)
        end
    end
    self.qrpanel:setVisible(b)
    -- self.nameLbl:setVisible(b)
    -- self.lvLbl:setVisible(b)
    -- self.zoneLbl:setVisible(b)
end

function PhotoView:addFrame()
    self.photoFrame = ccui.ImageView:create("ui_resources/photo/photoframe_1.png",1)
    self.filmFrame = ccui.ImageView:create("ui_resources/photo/photoframe_2.png",1)
    self.photoFrame:setCapInsets(cc.rect(26,26,26,26))
    self.filmFrame:setCapInsets(cc.rect(0,70,200,60))

    self.photoFrame:setScale9Enabled(true)
    self.filmFrame:setScale9Enabled(true)

    self.photoFrame:setAnchorPoint(cc.p(0, 0))
    self.filmFrame:setAnchorPoint(cc.p(0, 0))

    self:addChild(self.photoFrame, -1)
    self:addChild(self.filmFrame, -1)

    self.photoFrame:setContentSize(cc.size(display.width, display.height))
    self.filmFrame:setContentSize(cc.size(display.width, display.height))

    self.photoFrame:hide()
    self.filmFrame:hide()
end

function PhotoView:setLogoType(id)
    local l = 200
    local r = display.width - l
    local b = 100
    local t = display.height - b
    local hm = display.width * 0.5
    local vm = display.height * 0.5

    local poss = {
        cc.p(l, t), cc.p(l, b),
        cc.p(r, t), cc.p(r, b),
        cc.p(hm, t), cc.p(hm, b),
        cc.p(l, vm), cc.p(r, vm),
    }

    if poss[id] then
        self.logo:show()
        self.logo:setPosition(poss[id])
    else
        self.logo:hide()
    end
end

function PhotoView:setFrameType(id)
    self.photoFrame:hide()
    self.filmFrame:hide()

    if id == 2 then
        self.filmFrame:show()
    elseif id == 3 then
        self.photoFrame:show()
    end
end


-- function PhotoView:getViewType()
-- 	return app.mvc.VIEWTYPE_BLUR
-- end

return PhotoView